package dataAccess;

import businessEntities.MainLogger;
import businessEntities.TestQueryInformation;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DALQueryTester {

    /**
     * Test simple where query - SELECT * FROM <code>databaseName</code>.<code>tableName</code> WHERE <code>where</code>.
     * @param databaseName
     * @param tableName
     * @param where
     * @return TestQueryInformation that holds the information about the test.
     * @throws ConnectionException if the connection to the database is not reachable.
     */
    public TestQueryInformation testWhereQuery(String databaseName, String tableName, String where) {
        Connection connection = SingeltonConnection.getConnection();
        String query = "SELECT * FROM " + databaseName + "." + tableName + " WHERE " + where;

        try (Statement stm = connection.createStatement()) {

            MainLogger.logger.info("Execute Test. Query : [" + query + "].");
            ResultSet rs = stm.executeQuery(query);

        } catch (SQLException ex) {
            MainLogger.logger.warn("Test Query : [" + query + "] - FAILED. Error Message : " + ex.getMessage());
            return new TestQueryInformation(ex.getMessage(), query, false);
        }

        MainLogger.logger.info("Test Query : [" + query + "] - PASSED.");
        return new TestQueryInformation("", query, true);
    }
}
